﻿#include "Dialog_SetRunningMode.h"
#include "ui_Dialog_SetRunningMode.h"

#include "DeviceInfoManager.h"
#include "Device.h"

Dialog_SetRunningMode::Dialog_SetRunningMode(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog_SetRunningMode)
{
  ui->setupUi(this);
  setFixedSize(size());
  radioButton_positionMode=ui->radioButton_positionMode;
  radioButton_rateMode=ui->radioButton_rateMode;
  lineEdit_position=ui->lineEdit_position;
  lineEdit_rate=ui->lineEdit_rate;
  lineEdit_acceleration=ui->lineEdit_acceleration;
  connect(radioButton_positionMode,&QRadioButton::clicked,this,&Dialog_SetRunningMode::slot_updatePositionMode);
  connect(radioButton_rateMode,&QRadioButton::clicked,this,&Dialog_SetRunningMode::slot_updateRateMode);
}

Dialog_SetRunningMode::~Dialog_SetRunningMode()
{
  delete ui;
}

int Dialog_SetRunningMode::exec()
{
  slot_updateDeviceInfo();
  return QDialog::exec();
}

bool Dialog_SetRunningMode::getRunningMode()
{
  return radioButton_rateMode->isChecked();
}

float Dialog_SetRunningMode::getPosition()
{
  return lineEdit_position->getPosition();
}

float Dialog_SetRunningMode::getRate()
{
  return lineEdit_rate->getRate();
}

float Dialog_SetRunningMode::getAcceleration()
{
  return lineEdit_acceleration->getAcceleration();
}

void Dialog_SetRunningMode::accept()
{
  QDialog::accept();
}
void Dialog_SetRunningMode::reject()
{
  slot_updateDeviceInfo();
  QDialog::reject();
}

void Dialog_SetRunningMode::slot_updateDeviceInfo()
{
  Device* device=deviceInfoManager->getDeviceInfoByDeviceId(deviceId);
  if(Q_NULLPTR==device)//没有对应设备ID的信息
    {
      return;
    }
  if(device->getTurnTable_runningMode())//true代表速率模式，false代表位置模式
    {//速率模式
      radioButton_positionMode->setChecked(false);
      radioButton_rateMode->setChecked(true);
      lineEdit_position->setText("---");
    }
  else//位置模式
    {
      radioButton_positionMode->setChecked(true);
      radioButton_rateMode->setChecked(false);
      lineEdit_position->setPosition(device->getTurnTable_targetPosition());
    }
  lineEdit_rate->setRate(device->getTurnTable_targetRate());
  lineEdit_acceleration->setAcceleration(device->getTurnTable_targetAcceleration());
}

void Dialog_SetRunningMode::slot_updatePositionMode()
{
  if (radioButton_positionMode->isChecked())
    {
      lineEdit_position->setDisabled(false);
      lineEdit_rate->turnMode(LineEdit_TurnTableRate::PositionMode);
      //      ui.lineEdit_position->setPosition(turnTableRunningStateManager->getState_turnTable_targetPosition());
      //      ui.lineEdit_rate->setRate(turnTableRunningStateManager->getState_turnTable_targetRate());
      //      ui.lineEdit_acceleration->setAcceleration(turnTableRunningStateManager->getState_turnTable_targetAcceleration());
      return;
    }
}
void Dialog_SetRunningMode::slot_updateRateMode()
{
  if (radioButton_rateMode->isChecked())
    {
      ui->lineEdit_position->setText("---");
      ui->lineEdit_position->setDisabled(true);
      ui->lineEdit_rate->turnMode(LineEdit_TurnTableRate::RateMode);
      //      ui.lineEdit_rate->setRate(turnTableRunningStateManager->getState_turnTable_targetRate());
      //      ui.lineEdit_acceleration->setAcceleration(turnTableRunningStateManager->getState_turnTable_targetAcceleration());
      return;
    }
}

uint Dialog_SetRunningMode::getDeviceId()
{
  return deviceId;
}

Dialog_SetRunningMode* Dialog_SetRunningMode::setDeviceId(uint deviceId)
{
  this->deviceId=deviceId;
  return this;
}

DeviceInfoManager* Dialog_SetRunningMode::getDeviceInfoManager()const
{
  return deviceInfoManager;
}

Dialog_SetRunningMode* Dialog_SetRunningMode::setDeviceInfoManager(DeviceInfoManager* const deviceInfoManager)
{
  this->deviceInfoManager=deviceInfoManager;
  printInjection(this,deviceInfoManager);
  return this;
}

void Dialog_SetRunningMode::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setDeviceInfoManager(getObjFromMap<DeviceInfoManager>(objMap));
}
